#include "Image.h"

Image::Image(const std::string &imagePath) throw (ImagePathNotFoundException){
	this->image = IMG_Load(imagePath.c_str());

	if ( this->image == NULL ) throw  ImagePathNotFoundException("El path de la imagen no fue encontrado");

	this->imageWidth = this->image->w;
	this->imageHeight = this->image->h;
	this->imageCenter = new Point(floor(this->image->w/2),floor(this->image->h/2));
	this->imagePath = imagePath;
	figureWidth = 0;
	figureHeight = 0;
	figureCenter = NULL;

	//la imagen no puede ser superior a 1000 * 1000/
	if (this->imageHeight >= 1000)
			imageHeight = 999;
	if (this->imageWidth >= 1000)
			imageWidth = 999;
}


Image::Image() { 
	this->image = NULL;
	imageWidth = 0;
	imageHeight = 0;
	imageCenter = NULL;
	figureWidth = 0;
	figureHeight = 0;
	figureCenter = NULL;
}


Image::~Image(){
	
	if(this->image){
		SDL_FreeSurface(this->image);
	}
	if(this->imageCenter)
	{
		delete (this->imageCenter);
	}
	if(this->figureCenter)
	{
		delete (this->figureCenter);
	}
}


Color* Image::getColor(Point* punto){
	Color* color = NULL;

	int posicionEnArray = (imageWidth * imageHeight) - (imageWidth * punto->getY() + punto->getX());
	color = new Color();
    int tamanioArray = abs(this->getImageWidth()) * abs (this->getImageHeight());  

	if(!error && posicionEnArray>0 && posicionEnArray<tamanioArray){
		color->setRed(rarray[posicionEnArray]);
		color->setGreen(garray[posicionEnArray]);
		color->setBlue(barray[posicionEnArray]);
		color->setAlpha(255); //seteo sin transparecia ->totalemente opaco
	}
		
	return color;
}


bool Image::validarLimites(std::vector<Point*> *puntos){

	//recorro una a una las figuras y las voy dibujando
   	std::vector<Point*>::iterator it = puntos->begin();
	while(it != puntos->end())
	{
		if (((this->imageWidth) < ((*it)->getX())) || ((*it)->getX() < 0) || ((this->imageHeight) < ((*it)->getY())) ||  ((*it)->getY() < 0 ) )
			return false;
		it++;	
	}

	return true;
}


Color* Image::getColor(int x,int y){
	Point* p = new Point(x,y);
	return getColor(p);
}


SDL_Surface* Image::getImagen(){
	return this->image;
}
